//[messageId, tree, complete]
var globalTargetTree = null;

function ReplyTree(){
	this.roots = new Array();
	this.trees = new Array();

	this.com = new Communicator();
	this.com.addParameter('type','message');
	this.com.addParameter('task','getTrackToRoot');

	this.room = global.getObject('room').id;

	global.createObject('replyTree', this);
}

ReplyTree.prototype.addTree = function(root, tree){
	if(isObject(root)) root = root.getKey();
	else root = 'message_'+root;

	this.trees[root] = tree;
}

ReplyTree.prototype.addRoot = function(message, root){
	if(isObject(message)) message = message.getKey();
	else message = 'message_'+message;

	this.trees[message] = root;
}

ReplyTree.prototype.showReplyTree = function(messageId){
	this.com.addParameter('data',JSON.stringify({message: messageId, room: this.room}));
	this.com.sendRequest(checkMessagesInTree);

	globalTargetTree = [messageId, null, false];
}

function showReplyTree(event){
	var caller = event.target;
	var message = findParentWithClass(caller, 'message');

	var messageId = getData(message,'id');
	var userId = getData(message,'user');
	var text = message.getElementsByClassName('text')[0].innerHTML;
	var user = global.getObject('user', 'user_'+userId);
	
	var replyTree = global.getObject('replyTree');
	replyTree.showReplyTree(messageId);
	handleShading(event, 'replyTree');
}


function checkMessagesInTree(response){
	var obj = eval(response);
	
	var message = obj[0];
	var root = obj[1];
	var replyTree = global.getObject('replyTree');
	//

	if(globalTargetTree[0] == message){
		var tree = obj[2];
		globalTargetTree[1] = tree;
		replyTree.addTree(root, tree);
		
		var requestMessages = new Array();

		var id, message;
		for(var i=0, iMax=tree.length; i<iMax; i++){
			id = tree[i];
			message = global.getObject('message','message_'+id);

			replyTree.addRoot(id,root);

			if(!message){
				requestMessages.push(id);
			}
		}

		if(requestMessages.length > 0){
			var room = global.getObject('room');
			var com = new Communicator();
			com.addParameter('type','message');
			com.addParameter('task','getMessages');
			com.addParameter('data',JSON.stringify({ids: requestMessages, room: room.id}));
			com.sendRequest(receiveMessages);
		}else globalTargetTree[2] = true;

		createMessageTree();
	}	
}

function createMessageTree(){
	if(globalTargetTree == null) return false; //desistiu

	if(globalTargetTree[2]){ //recebeu
		var tree = globalTargetTree[1];

		var key, message;
		var fragment = document.createDocumentFragment();
		for(var i=0, iMax=tree.length; i<iMax; i++){
			key = 'message_'+tree[i];

			message = global.getObject('message',key);

			fragment.appendChild(message.createMessageItem());
		}

		var replyTree = global.getElement('replyTree');
		var list = global.getElement('replyTreeList');
		clearElement(list);
		list.appendChild(fragment);
		
		if(list.offsetHeight > replyTree.offsetHeight){
			addClass(replyTree,'scroll');
		}else{
			removeClass(replyTree,'scroll');
		}
		
		globalTargetTree = null;

		var wrapper = replyTree.getElementsByClassName('wrapper')[0];
		wrapper.scrollTop = wrapper.scrollHeight;
	}else{ //se ainda for falso, espera 1 segundo e chama novamente
		setTimeout(function(){
			createMessageTree();
		},1000);
	}	
}

function receiveMessages(response){
	var obj = eval(response);

	for(var i=0, iMax=obj.length; i<iMax; i++){
		var data = obj[i];

		var message = new Message(data[0]);
		message.assignProperties(data[1],data[2],data[3],data[4]);
	}

	if(targetTree != null) targetTree[2] = true;
}

function openReplyTree(){
	var image = new Image();

	image.onload = function(){
		var list = global.getElement('replyTreeList');
		if(list.innerHTML == ''){
			list.appendChild(image);
		}	
	}

	image.src = 'image/loader.gif';
	image.className = 'loader';
}

function closeReplyTree(){
	clearElement(global.getElement('replyTreeList'));
}